/**
 * This object handler database information.
 * Contains the functions to get, set, update and consult data.
 * 
 * @author Marcelo A. Nagatomo - RA 062689
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "DataHandler.h"
#include "DebugUtils.h"

// Number of movies
int moviesSize;

// List of movies
MoviesList movies;

/**
 * Get a Movie struct from string.
 */
Movie createMovie(char* registry);

/**
 * Find the file wich contains database informations.
 * @param 	fileName a string name for the file
 * @return 	FILE* file fileName.dat
 */
FILE* findDataSource(char* fileName){
	FILE* fp;
	
	if((fp = fopen(fileName, "r+")) == NULL ){
		perror("An error occurred while create/open a file database.");
		exit(-1);
	}
	
	return fp;
}


/**
 * Get movies from database.
 * @param 	fp a string name for the file
 * @return 	FILE* file fileName.dat
 */
MoviesList getMovies(FILE* fp){
	moviesSize = 0;
	char buffer[LINE_SIZE];
	
	fscanf(fp, "%d\n", &moviesSize);
	MoviesList movies = (MoviesList) malloc(moviesSize*sizeof(Movie));
	
	int i;
	for(i=0; i<moviesSize; i++){
		fscanf(fp, "%[^\n]%*c", buffer);
		buffer[LINE_SIZE-1] = '\0';
		
		movies[i] = createMovie(buffer);
	}
	
	return movies;
}


/**
 * Get a Movie struct from string.
 */
Movie createMovie(char* registry){
	Movie movie;

	if(registry != NULL){
		int size = 0;
		
		char *str = strtok(registry, "|");
		movie.id = atoi(str);
	
		str = strtok(NULL, "|");		
		size = strlen(str);
		movie.title = (char*) malloc(sizeof(char)*size);
		strcpy(movie.title, str);
		
		str = strtok(NULL, "|");
		size = strlen(str);
		movie.sinopse = (char*) malloc(sizeof(char)*size);
		strcpy(movie.sinopse, str);
				
		str = strtok(NULL, "|");
		size = strlen(str);
		movie.room = (char*) malloc(sizeof(char)*size);
		strcpy(movie.room, str);
				
		str = strtok(NULL, "|");
		size = strlen(str);
		movie.hours = (char*) malloc(sizeof(char)*size);
		strcpy(movie.hours, str);
		
		str = strtok(NULL, "|");
		movie.rate = atof(str);
		
		str = strtok(NULL, "|");
		movie.clients = atoi(str);
		
		movie.valid = TRUE;
	}
	
	return movie;
}


/**
 * Initialize the data handler.
 */
void initDataHandler(){
	FILE* fp = findDataSource(DATABASE_NAME);
 	movies = getMovies(fp);
}


/**
 * Search for a movie by id.
 * @param 	int id of movie
 * @return 	Movie
 */
Movie searchMovie(int id){
	Movie movie;
	movie.valid = FALSE;
	
	int i;
	for(i=0; i<moviesSize; i++){
		if(movies[i].id == id){
			movie = movies[i];
			movie.valid = movies[i].valid;
		}
	}
	
	return movie;
}


/**
 * Create a string with all info of movie.
 * @param 	Movie movie structure
 * @return 	char* "id|title|sinopse|room|hours|rate|clients|valid"
 */
char* getStrMovieInfo(Movie movie){
	char* str;
	char temp[10];
	int size = 0;
	
	sprintf(temp,"%d|",movie.id);
	size += strlen(temp);
	size += strlen(movie.title)+1;
	size += strlen(movie.sinopse)+1;
	size += strlen(movie.room)+1;
	size += strlen(movie.hours)+1;
	sprintf(temp,"%f|",movie.rate);
	size += strlen(temp);
	sprintf(temp,"%d|",movie.clients);
	size += strlen(temp);
	sprintf(temp,"%d",movie.valid);
	size += strlen(temp);
		
	str = malloc(size);
		
	sprintf(temp,"%d|",movie.id);
	strcat(str, temp);
	strcat(str, movie.title);
	strcat(str, "|");
	strcat(str, movie.sinopse);
	strcat(str, "|");
	strcat(str, movie.room);
	strcat(str, "|");
	strcat(str, movie.hours);
	strcat(str, "|");
	sprintf(temp,"%f|",movie.rate);
	strcat(str, temp);
	sprintf(temp,"%d",movie.clients);
	strcat(str, temp);
// 	sprintf(temp,"%d",movie.valid);
// 	strcat(str, temp);
		
	return str;
}



/**
 * Get id and title of all movies.
 * @param 	fp a string name for the file
 * @return 	char* with data to send to client
 */
char* getNumberMoviesList(){
	char* response;
	int i, size = 0;;
	char temp[10];
	
	sprintf(temp,"%d",moviesSize);
	size += strlen(temp);
	for(i=0; i<moviesSize; i++){
		sprintf(temp,"|%d|",movies[i].id);
		size += strlen(temp);
		size += strlen(movies[i].title);
	}
	
	response = malloc(size);
	
	sprintf(temp,"%d",moviesSize);
	strcat(response, temp);
	for(i=0; i<moviesSize; i++){
		sprintf(temp,"|%d|",movies[i].id);
		strcat(response, temp);
		strcat(response, movies[i].title);
	}
		
	return response;
}


/**
 * Get all info about the movies.
 * 
 * @return 	char* with data to send to client
 */
char* getAllInfoMoviesList(){
	char* response;
	int i, size = 0;;
	char temp[10];
	char** strMovies = malloc(sizeof(char*)*moviesSize);
	
	sprintf(temp,"%d",moviesSize);
	size += strlen(temp);
	for(i=0; i<moviesSize; i++){
		strMovies[i] = getStrMovieInfo(movies[i]);
		size += strlen(strMovies[i])+1;
		//printf("strMovies: [%s]\n", strMovies[i]);	
	}
	
	response = malloc(size);
	
	sprintf(temp,"%d",moviesSize);
	strcat(response, temp);
	for(i=0; i<moviesSize; i++){
		strcat(response, "|");
		strcat(response, strMovies[i]);
	}
		
	return response;
}


/**
 * Get sinopse of movie.
 * @param 	int id movie
 * @return 	char* with data to send to client
 */
char* getSinopseMovie(int id){
	Movie movie = searchMovie(id);
	
	if(movie.valid == TRUE)
		return movie.sinopse;
	return MOVIE_NOT_FOUND;
}


/**
 * Get all information about one movie.
 * @param 	int id movie
 * @return 	char* with data to send to client
 */
char* getAllInfoMovie(int id){
	char* response;
	Movie movie = searchMovie(id);
	
	if(movie.valid == TRUE){
		response = getStrMovieInfo(movie);
		return response;
	}
		
	return MOVIE_NOT_FOUND;
}


/**
 * Get the medium of movie.
 * @param 	int id movie
 * @return 	char* with data to send to client
 */
char* getMediumMovie(int id){
	char* response;
	char temp[10];
	char temp2[10];
	Movie movie = searchMovie(id);
	
	if(movie.valid == TRUE){
		sprintf(temp, "|%f", movie.rate);
		sprintf(temp2, "|%d", movie.clients);
		response = malloc(strlen(temp) + strlen(movie.title) + strlen(temp));
		
		strcpy(response, movie.title);
		strcat(response, temp);
		strcat(response, temp2);
		
		return response;			
	}
	
	return MOVIE_NOT_FOUND;
}


/**
 * Set the rate of movie.
 * @param 	Request request option from client
 * @return 	char* with data to send to client
 */
char* setRateMovie(Request req){
	char* response = OPT_ERROR;
	return response;
}

// 



// int main(int argc, char** argv){
// 	
// 	FILE* fp = findDataSource("database.dat");
// 	MoviesList movies = getMovies(fp);
// 	printMovies(movies, moviesSize, DEBUG);
// 
// 	return 0;
// }


